package co.fitstart.signin.dao;

import java.util.List;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

import co.fitstart.entity.coach.CoachSigninStatistic;
import co.fitstart.entity.coach.CoachStudentRelation;
import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.course.schedule.ScheduleStatus;
import co.fitstart.entity.course.schedule.StudentSigninRecord;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.bwh.BwhRecord;
import co.fitstart.entity.user.inbody.InbodyRecord;

/**
 * 
 * @author dan
 *
 */
@Dao("hibernate")
public interface SigninDao {
    
    /**
     * 
     * @return
     */
    @Query("FROM StudentSigninRecord")
    public List<StudentSigninRecord> list();
    
    /**
     * 
     * @param siteId
     * @param message
     * @return
     */
    @Query("FROM StudentSigninRecord WHERE siteId = :siteId AND showed = false AND message = :message ORDER BY crateTime DESC")
    public StudentSigninRecord load(@Variable("siteId") Long siteId, @Variable("message") String message);
    
    /**
     * 
     * @param courseRound
     * @param scheduleStatus
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM CourseSchedule WHERE disabled = false AND courseRound = :courseRound AND scheduleStatus in ( :scheduleStatus) ORDER BY occurDate DESC")
    public List<CourseSchedule> listCourseSchedule(@Variable("courseRound") CourseRound courseRound, @Variable("scheduleStatus") List<ScheduleStatus> scheduleStatus, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    /**
     * 
     * @param courseRound
     * @param scheduleStatus
     * @return
     */
    @Query("SELECT COUNT(*) FROM CourseSchedule courseSchedule WHERE courseSchedule.disabled = false AND courseSchedule.courseRound = :courseRound AND courseSchedule.scheduleStatus = :scheduleStatus")
    public int countFinishedSchedule(@Variable("courseRound")CourseRound courseRound, @Variable("scheduleStatus")ScheduleStatus scheduleStatus);
    
    /**
     * load CoachStudentRelation
     * 
     * @param coach
     * @param courseType
     * @param student
     * @return
     */
    @Query("SELECT coachStudentRelation FROM CoachStudentRelation coachStudentRelation WHERE coachStudentRelation.coach = :coach AND coachStudentRelation.courseType = :courseType AND coachStudentRelation.student = :student")
    public CoachStudentRelation loadCoachStudentRelation(@Variable("coach")Principal coach, @Variable("courseType")CourseType courseType, @Variable("student")Student student);
    
    /**
     * 
     * @param courseRound
     * @return
     */
    @Query("SELECT COUNT(*) FROM CourseSchedule courseSchedule WHERE courseSchedule.disabled = false AND courseSchedule.courseRound = :courseRound")
    public int countAllSchedule(@Variable("courseRound")CourseRound courseRound);
    
    /**
     * 
     * @param courseRound
     * @return
     */
    @Query("FROM CourseSchedule WHERE disabled = false AND courseRound = :courseRound AND occurDate IS NOT NULL ORDER BY occurDate DESC")
    public List<CourseSchedule> listCourseSchedule(@Variable("courseRound") CourseRound courseRound);
    
    
    @Query("FROM CourseSchedule WHERE disabled = false AND occurDate IS NOT NULL AND student = :student AND scheduleStatus = :scheduleStatus ORDER BY occurDate ASC")
    public CourseSchedule loadFirstFinish(@Variable("student") Student student, @Variable("scheduleStatus") ScheduleStatus scheduleStatus);
    
    /**
     * 
     * @param user
     * @param start
     * @param end
     * @return
     */
    @Query("FROM BwhRecord WHERE user = :user AND recordTime BETWEEN :start AND :end AND disabled = false ORDER BY recordTime DESC")
    public List<BwhRecord> listBwhRecord(@Variable("user") User user, @Variable("start") String start, @Variable("end") String end);
    
    /**
     * @param user
     * @return
     */
    @Query("FROM InbodyRecord WHERE disabled = false AND user = :user AND recordTime BETWEEN :start AND :end ORDER BY recordTime DESC")
    public List<InbodyRecord> listInbodyRecord(@Variable("user") User user, @Variable("start") String start, @Variable("end") String end);

    /**
     * 
     * @param coach
     * @param month
     * @return
     */
    @Query("FROM CoachSigninStatistic WHERE principal = :coach AND month = :month")
    public CoachSigninStatistic loadCoachSigninStatistic(@Variable("coach") Principal coach, @Variable("month") String month);
}
